/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;
import pojo.ChauLuc;
import pojo.ChiTietDonDatHang;
import pojo.DonDatHang;
import pojo.HinhThucThanhToan;
import pojo.LoaiTien;
import pojo.SanPham;
import pojo.TinhTrangDonHang;
import service.ChauLucService;
import service.ChucNangService;
import service.LoaiTienService;
import service.DonDatHangService;
import service.HinhThucThanhToanService;

/** 
 *
 * @author MongKieu
 */
public class ThongTinKhachHangController extends SimpleFormController {

    private LoaiTienService loaiTienService;
    private ChauLucService chauLucService;
    private ChucNangService chucNangService;
    private DonDatHangService donDatHangService;
    private HinhThucThanhToanService hinhThucThanhToanService;
    private TinhTrangDonHang tinhTrangDonHang;

    public ThongTinKhachHangController() {
        //Initialize controller properties here or 
        //in the Web Application Context

        setCommandClass(DonDatHang.class);
        setCommandName("donDatHang");
        //setSuccessView("successView");
        setFormView("ThongTinKhachHang");
    }

    @Override
    public ModelAndView showForm(HttpServletRequest request, HttpServletResponse response, BindException errors) throws Exception {
        ModelAndView mv = super.showForm(request, response, errors);
        List<LoaiTien> listLoaiTien = loaiTienService.listLoaiTien();
        List<ChauLuc> listChauLuc = chauLucService.listChauLuc();
        List<HinhThucThanhToan> listHinhThucThanhToan = hinhThucThanhToanService.listHinhThucThanhToan();
        mv.addObject("listLoaiTiens", listLoaiTien);
        mv.addObject("listChauLucs", listChauLuc);
        mv.addObject("listHinhThucThanhToans", listHinhThucThanhToan);
        mv.addObject("message", "0");
        mv.addObject("ThongBao", "Vao Thanh Toan");
        return mv;
    }

    @Override
    protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object command, BindException errors) throws Exception {
        HttpSession session = request.getSession();
        ModelAndView mv = new ModelAndView();
        try {

            mv = new ModelAndView("DonDatHang");
            mv.addObject("ThongBao", "Xac Nhan Don Hang");
            mv.addObject("message", "1");

            DonDatHang donDatHang = (DonDatHang) command;
            tinhTrangDonHang.setId(1);
            donDatHang.setTinhTrang(tinhTrangDonHang);
            mv.addObject("DonDatHang", donDatHang);
            session.setAttribute("DonDatHang", donDatHang);

            ArrayList<SanPham> gioHang = null;
            gioHang = (ArrayList<SanPham>) session.getAttribute("GioHang");

            //Giỏ hàng trống
            if (gioHang == null) {
                return new ModelAndView("GioHang");
            } else if (gioHang.isEmpty()) {
                return new ModelAndView("GioHang");
            }

            mv.addObject("listSanPham", gioHang);

            double tongGiaTri = 0;
            for (int i = 0; i < gioHang.size(); i++) {
                SanPham sp = gioHang.get(i);
                tongGiaTri = tongGiaTri + sp.getSoLuongDangCo() * sp.getGiaBanHienTai();
            }
            mv.addObject("TongTien", tongGiaTri);
        } catch (Exception e) {
            mv = new ModelAndView("ThongBaoLoi");
            mv.addObject("messageError", "Xin chào bạn, thông tin tìm kiếm không hợp lệ.");
        }
        return mv;
    }

    public ChauLucService getChauLucService() {
        return chauLucService;
    }

    public void setChauLucService(ChauLucService chauLucService) {
        this.chauLucService = chauLucService;
    }

    public ChucNangService getChucNangService() {
        return chucNangService;
    }

    public void setChucNangService(ChucNangService chucNangService) {
        this.chucNangService = chucNangService;
    }

    public DonDatHangService getDonDatHangService() {
        return donDatHangService;
    }

    public void setDonDatHangService(DonDatHangService donDatHangService) {
        this.donDatHangService = donDatHangService;
    }

    public HinhThucThanhToanService getHinhThucThanhToanService() {
        return hinhThucThanhToanService;
    }

    public void setHinhThucThanhToanService(HinhThucThanhToanService hinhThucThanhToanService) {
        this.hinhThucThanhToanService = hinhThucThanhToanService;
    }

    public LoaiTienService getLoaiTienService() {
        return loaiTienService;
    }

    public void setLoaiTienService(LoaiTienService loaiTienService) {
        this.loaiTienService = loaiTienService;
    }

    public TinhTrangDonHang getTinhTrangDonHang() {
        return tinhTrangDonHang;
    }

    public void setTinhTrangDonHang(TinhTrangDonHang tinhTrangDonHang) {
        this.tinhTrangDonHang = tinhTrangDonHang;
    }
}
